import 'package:demo/widget/keyboard/number_keyboard.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NumberKeyboardExample extends StatefulWidget {
  const NumberKeyboardExample({Key? key}) : super(key: key);

  @override
  NumberKeyboardExampleState createState() => NumberKeyboardExampleState();
}

class NumberKeyboardExampleState extends State<NumberKeyboardExample> {
  final TextEditingController _textEditingController = TextEditingController();
  bool _isShowKeyboard = false;

  @override
  void dispose() {
    _textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("数字键盘")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const Text('输入安全密码'),
            Builder(
              builder: (context) {
                return TextField(
                  readOnly: true,
                  showCursor: true,
                  onTap: () => _openNumberKeyboard(context),
                  controller: _textEditingController,
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  void _openNumberKeyboard(context) {
    if (_isShowKeyboard) return;
    _isShowKeyboard = true;
    showBottomSheet(
      backgroundColor: Colors.white,
      context: context,
      builder: (context) {
        return NumberKeyboard(
          onButtonResultClickListener: _onButtonResultClickListener,
          onRightButtonClickListener: _onRightButtonClickListener,
          onLeftButtonClickListener: _onLeftButtonClickListener,
        );
      },
    );
  }

  void _onButtonResultClickListener(String text) {
    final List<String> currentText = _textEditingController.text.split("");
    final TextSelection selection = _textEditingController.selection;
    int offset = text.length;
    if (currentText.isEmpty) {
      currentText.add(text);
    } else {
      currentText.insert(selection.baseOffset, text);
      offset = selection.baseOffset + text.length;
    }
    _textEditingController.value = TextEditingValue(
      text: currentText.join(""),
      selection: TextSelection.fromPosition(TextPosition(offset: offset)),
    );
  }

  void _onRightButtonClickListener() {
    if (_textEditingController.text.length <= 1) {
      if (_textEditingController.text != "") _textEditingController.text = "";
    } else {
      final TextSelection selection = _textEditingController.selection;
      if (selection.baseOffset <= 0) {
        return;
      }
      final int offset = selection.baseOffset - 1;
      final List<String> currentText = _textEditingController.text.split("");
      currentText.removeAt(offset);
      _textEditingController.value = TextEditingValue(
        text: currentText.join(""),
        selection: TextSelection.fromPosition(TextPosition(offset: offset)),
      );
    }
  }

  void _onLeftButtonClickListener() {
    _isShowKeyboard = false;
    Navigator.of(context).pop();
    FocusScope.of(context).requestFocus(FocusNode());
  }
}
